{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from model.model import Model\n",
    "import numpy as np\n",
    "from os import makedirs\n",
    "from os.path import join, isdir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "checkpoint_path = \"results/000022/checkpoints/29.pt\"\n",
    "model = torch.load(checkpoint_path)\n",
    "t = checkpoint_path.split('/')\n",
    "i1, i2 = t[-3], t[-1][0:-3]\n",
    "i = i1 + \"_\" + i2\n",
    "if not isdir('weights'):\n",
    "    makedirs('weights')\n",
    "base_dir = join(\"weights\",i)\n",
    "makedirs(base_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "for index, param in enumerate(model.parameters()):\n",
    "    layer = index // 2\n",
    "    weight_type = None\n",
    "    if index % 2 == 0:\n",
    "        weight_type = \"weight\"\n",
    "    else:\n",
    "        weight_type = 'bias'\n",
    "    with open(join(base_dir,weight_type+\"_\"+str(layer)+\".txt\"),'w') as f:\n",
    "        # print(param.shape)\n",
    "        for i in param.view(-1,):\n",
    "            f.write(str(i.item()) + ' ')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "29e1e0f433528b30d0029eb6eb3077f94c7f86e16287bd20a9728025e03018be"
  },
  "kernelspec": {
   "display_name": "Python 3.6.13 64-bit ('hp': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
